<?php

require_once "/home/viridiana/sbdev18/www/drive/google-api-php-client/src/Google_Client.php";
require_once "/home/viridiana/sbdev18/www/drive/google-api-php-client/src/contrib/Google_DriveService.php";
require_once "/home/viridiana/sbdev18/www/drive/google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = '822260332168-krehinm0c39jf9fbp2kdnfn4177v9tf0@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = './515713ceb88b0c43f7b33cad28010f1a5b68789b-privatekey.p12';
/**
 * Build and returns a Drive service object authorized with the service accounts.
 *
 * @return Google_DriveService service object.
 */
function buildService($SERVICE_ACCOUNT_PKCS12_FILE_PATH, $SERVICE_ACCOUNT_EMAIL , $DRIVE_SCOPE ) {
  $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
  $auth = new Google_AssertionCredentials(
      $SERVICE_ACCOUNT_EMAIL,
      array($DRIVE_SCOPE),
      $key);
  $client = new Google_Client();
  $client->setUseObjects(true);
  $client->setAssertionCredentials($auth);
  return new Google_DriveService($client);
}

function createPublicFolder($service, $folderName, $parentId) {
  $file = new Google_DriveFile();
  $file->setTitle($folderName);
  $file->setMimeType('application/vnd.google-apps.folder');
  if ($parentId != null) {
    $parent = new Google_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
  }
  $createdFile = $service->files->insert($file, array(
      'mimeType' => 'application/vnd.google-apps.folder',
  ));

  $permission = new Google_Permission();
  $permission->setValue('');
  $permission->setType('anyone');
  $permission->setRole('reader');

 $service->permissions->insert(
      $createdFile->getId(), $permission);

  return $file;
}

function retrieveAllFiles($service) {
  $result = array();
  $pageToken = NULL;

  do {
    try {
      $parameters = array();
      if ($pageToken) {
        $parameters['pageToken'] = $pageToken;
      }
      $files = $service->files->listFiles($parameters);

      $result = array_merge($result, $files->getItems());
      $pageToken = $files->getNextPageToken();
    } catch (Exception $e) {
      print "An error occurred: " . $e->getMessage();
      $pageToken = NULL;
    }
  } while ($pageToken);
  return $result;
}

$service = buildService($SERVICE_ACCOUNT_PKCS12_FILE_PATH, $SERVICE_ACCOUNT_EMAIL, $DRIVE_SCOPE );
$all_files = retrieveAllFiles($service);
$file =$all_files[1];
#$child = createPublicFolder($service, 'Audio', $file->id);

$all_files = retrieveAllFiles($service);
$file =$all_files[0];
print_r ($file->id);
echo "----";
print_r ($file->title);
echo "----";
print_r ($file->webViewLink);
echo "\n";
$file =$all_files[1];
print_r ($file->id);
echo "----";
print_r ($file->title);
echo "----";
print_r ($file->webViewLink);
echo "\n";
$file =$all_files[2];
print_r ($file->id);
echo "----";
print_r ($file->title);
echo "----";
print_r ($file->webViewLink);
echo "\n";
?>
